/*PhotoGallery.java
 * Draws the main photo gallery and select mode gallery
 * 
 * Gallery code based on http://developer.android.com/resources/tutorials/views/hello-gridview.html
 * Shared Preferences code based on example from http://developer.android.com/guide/topics/data/data-storage.html
 * Alert dialog based on example from http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlertDialogSamples.html
 * Toast code based on example from http://developer.android.com/guide/topics/ui/notifiers/toasts.html
 */
package com.tagit;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

public class PhotoGallery extends Activity {

	private static final int ADD_TAG_ID = 0;
	private PhotosDbAdapter mPhotoDb;
	private static PhotoCursorAdapter mCursorAdapter;
	public static GridView gridview;
	public static int selectState; // Track whether in select mode or not
	public static final String PREFS_NAME = "MyPrefsFile"; // Store favorite tag
															// booleans and
															// strings here

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mPhotoDb = new PhotosDbAdapter(this);
		mPhotoDb.open();
		Cursor c = mPhotoDb.selectPhotos(30);

		setContentView(R.layout.photogallery);
		gridview = (GridView) findViewById(R.id.gridview);

		selectState = 0; // Start out not in select mode

		// Create a brand new photoAdapter
		mCursorAdapter = new PhotoCursorAdapter(this, c, selectState);
		mCursorAdapter.setState(0);
		MyApp.setAdapter(mCursorAdapter);
		gridview.setAdapter(mCursorAdapter);
		registerForContextMenu(gridview);

		// Set Listener for user clicking on photo (pulled out poorly)
		setPicListener();

		// Set Listener for user clicking on buttons
		setButtonClickListener();
		setPublishClickListener();
	}

	// Method for when resume gallery from other activity
	@Override
	protected void onResume() {
		super.onResume();

		// Reset the cursor
		Cursor c = mPhotoDb.selectPhotos(30);
		mCursorAdapter.setState(0); // Not in select mode
		mCursorAdapter = new PhotoCursorAdapter(this, c, selectState);
		MyApp.setAdapter(mCursorAdapter);
		gridview.setAdapter(mCursorAdapter);

		// Reset the listeners
		setPicListener();
		setButtonClickListener();
	}

	// Create the Preferences and Help menus
	private void CreateMenu(Menu menu) {
		menu.setQwertyMode(true);
		MenuItem mnu1 = menu.add(0, 0, 0, "Preferences");
		{
			mnu1.setAlphabeticShortcut('a');
		}
		MenuItem mnu2 = menu.add(0, 1, 1, "Help");
		{
			mnu2.setAlphabeticShortcut('b');
		}
	}

	private boolean MenuChoice(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			goToPreferences();
			return true;
		case 1:
			goToHelp();
			return true;
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		CreateMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return MenuChoice(item);
	}

	public void goToPreferences() {
		Intent intent = new Intent(PhotoGallery.this, Preferences.class);
		startActivity(intent);
	}

	public void goToHelp() {
		Intent intent = new Intent(PhotoGallery.this, Help.class);
		startActivity(intent);
	}

	// Set a listener for when a user short or long clicks on a photo
	public void setPicListener() {
		gridview.setOnItemClickListener(new OnItemClickListener() {

			// Operations for when user short clicks on photo
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				// If not in select mode, locate photo in db based on photo
				// position and open full screen view
				if (selectState == 0) {
					Intent intent = new Intent(PhotoGallery.this,
							ViewPhoto.class);
					Bundle extras = new Bundle();
					extras.putString(PhotosDbAdapter.KEY_URI,
							(String) mCursorAdapter.getItem(position));
					extras.putLong(PhotosDbAdapter.KEY_ID,
							mCursorAdapter.getItemId(position));
					intent.putExtras(extras);

					String imagePosition = "pos";

					intent.putExtra(imagePosition, position);
					startActivity(intent);
				}

				// If in select mode, add or remove to set of selected photos
				else {
					if (!(mCursorAdapter.isSelected(mCursorAdapter
							.getItemId(position)))) {
						mCursorAdapter.addSelected(mCursorAdapter
								.getItemId(position));
						gridview.setAdapter(mCursorAdapter);
					} else {
						mCursorAdapter.removeSelected(mCursorAdapter
								.getItemId(position));
						gridview.setAdapter(mCursorAdapter);
					}
				}
			}
		});

		// On long click, enter select mode and add photo user clicked on to
		// selected pics
		gridview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

				mCursorAdapter.addSelected(arg3);

				Intent intent = new Intent(arg0.getContext(), SelectMode.class);
				startActivity(intent);

				// Toast message to let know the user he is on select mode
				Context context = getApplicationContext();
				CharSequence text = "Entering select mode";
				int duration = Toast.LENGTH_LONG;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
				return true;
			}
		});
	}

	// Send user to Select mode when he clicks on 'Select' button
	public void setButtonClickListener() {
		Button selectButton = (Button) findViewById(R.id.select);
		selectButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), SelectMode.class);
				startActivity(intent);

				// Toast message to let know the user he is on select mode
				Context context = getApplicationContext();
				CharSequence text = "Entering select mode";
				int duration = Toast.LENGTH_LONG;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();

			}
		});
	}

	// Show dialog when user clicks on 'Publish' button. Wizard of Oz
	// implementation.
	public void setPublishClickListener() {
		Button publishButton = (Button) findViewById(R.id.publish);
		publishButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(v
						.getContext());
				builder.setTitle("Would you like to:");
				builder.setMultiChoiceItems(R.array.publishoptions,
						new boolean[] { false, false },
						new DialogInterface.OnMultiChoiceClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton, boolean isChecked) {
							}
						});
				builder.setPositiveButton("Continue",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								// In the future could do something with the
								// Flickr API here
							}
						});

				AlertDialog alert = builder.create();
				alert.show();
			}
		});
	}

	// Set select state (whether in select mode or not)
	public static void setSelectState(int state) {
		mCursorAdapter.setState(state);
	}

	// Get the current CursorAdapter
	public static PhotoCursorAdapter getAdapter() {
		return mCursorAdapter;
	}

}
